package business_opportunity

import (
	"context"
	"encoding/json"
	"errors"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/constants"
	pb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/business_opportunity"
	businessOpportunityBid "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/business_opportunity/bid"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/job_center"
	"git.myscrm.cn/golang/stark/v4"
)

type bidServer struct {
	businessBidService businessOpportunityBid.BidServiceIface
	jobService         job_center.JobCenterServiceIface
}

func NewBidServer(
	businessBidService businessOpportunityBid.BidServiceIface,
	jobService job_center.JobCenterServiceIface,
) pb.BidServiceServer {
	return &bidServer{
		businessBidService: businessBidService,
		jobService:         jobService,
	}
}

// SearchBid 搜索招中标
func (s *bidServer) SearchBid(ctx context.Context, request *pb.SearchBidRequest) (*pb.SearchBidResponse, error) {
	resp, err := s.businessBidService.SearchBid(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc SearchBid request:%+v, err:%s", request, err)
		return nil, err
	}
	return resp, nil
}

// FuzzySearchBid 模糊搜索招中标
func (s *bidServer) FuzzySearchBid(ctx context.Context, request *pb.FuzzySearchBidRequest) (*pb.FuzzySearchBidResponse, error) {
	resp, err := s.businessBidService.FuzzySearchBid(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc FuzzySearchProject FuzzySearchBid:%+v, err:%s", request, err)
		return nil, err
	}
	return resp, nil
}

// ExportSearchBid 搜索招中标-导出
func (s *bidServer) ExportSearchBid(ctx context.Context, request *pb.ExportSearchBidRequest) (*pb.ExportSearchBidResponse, error) {
	listResp, err := s.businessBidService.SearchBid(ctx, &pb.SearchBidRequest{
		Page:             1,
		PageSize:         1,
		OrganizingCode:   "",
		Keyword:          request.Keyword,
		SearchType:       request.SearchType,
		Id:               request.Id,
		SubId:            request.SubId,
		CompanyAreaCode:  request.CompanyAreaCode,
		Type:             request.Type,
		DataSourceCode:   request.DataSourceCode,
		CompanyType:      request.CompanyType,
		CompanyLabel:     request.CompanyLabel,
		PublishDateStart: request.PublishDateStart,
		PublishDateEnd:   request.PublishDateEnd,
		EndDateStart:     request.EndDateStart,
		EndDateEnd:       request.EndDateEnd,
	})
	//区域
	request.AreaCode, _ = s.businessBidService.FilterAreaCode(ctx, request.AreaCode)

	if err != nil {
		stark.Logger.Errorf(ctx, "grpc ExportSearchBid SearchBid request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	if listResp.Total == 0 {
		return nil, errors.New("导出数据为空")
	}
	if listResp.Total > 10000 {
		listResp.Total = 10000
	}

	requestBytes, _ := json.Marshal(request)

	jobId, err := s.jobService.NewJob(ctx, constants.JobMethodExport, constants.JobTypeMarketBidExport, listResp.Total, string(requestBytes))
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc ExportSaleList NewJob request:%+v, err:%s", request, err.Error())
		return nil, err
	}

	_ = s.jobService.StartJob(ctx, jobId)

	return &pb.ExportSearchBidResponse{JobId: jobId, IsSucceed: true}, nil
}

// BidDetail 招标详情
func (s *bidServer) BidDetail(ctx context.Context, request *pb.BidDetailRequest) (*pb.BidDetailResponse, error) {
	var errMsg string
	if request.GetId() == "" {
		errMsg = "id参数缺失"
	}
	if errMsg != "" {
		return nil, errors.New(errMsg)
	}
	res, err := s.businessBidService.BidDetail(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "BidDetail 获取招标详情失败，参数:%+v, 错误信息：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// BidDetailMobile 招标详情-获取手机号明文
func (s *bidServer) BidDetailMobile(ctx context.Context, request *pb.BidDetailMobileRequest) (*pb.BidDetailMobileResponse, error) {
	res, err := s.businessBidService.BidDetailMobile(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "BidDetailMobile 获取招标详情手机号失败，参数:%+v, 错误信息：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// WinBidDetail 中标详情
func (s *bidServer) WinBidDetail(ctx context.Context, request *pb.WinBidDetailRequest) (*pb.WinBidDetailResponse, error) {
	var errMsg string
	if request.GetId() == "" {
		errMsg = "id参数缺失"
	}
	if errMsg != "" {
		return nil, errors.New(errMsg)
	}
	res, err := s.businessBidService.WinBidDetail(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "WinBidDetail 获取中标详情失败，参数:%+v, 错误信息：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}
